<?php

namespace app\web\controller;

use app\common\controller\Web;
use app\common\model\Link;
use app\common\model\Resource;
use app\common\model\ResourceTopic;
use app\common\model\ResourceTopicRel;
use app\common\model\ResourceType;

/**
 * 资源中心
 * User: Feng
 * Date: 2024/05/29
 * @package app\home\controller
 */
class News extends Web
{
	//新闻中心
	public function news()
	{
		$type = $this->request->param('type', 1);
		// 当前页码
		$page = $this->request->param('page', 1);
		// 每页显示的数量
		$limit = 10;
		//热门
		$hotList = (new Resource())->where(['is_hot' => 1])->select();
		foreach ($hotList as $item) {
			$item['pic'] = _sImage($item['pic']);
		}
		//最新
		$newList = (new Resource())->where(['is_new' => 1])->select();
		foreach ($newList as $item) {
			$item['pic'] = _sImage($item['pic']);
		}
		//热门话题
		$topicIds = (new ResourceTopicRel())->column('topic_id');
		$topicIds = array_unique($topicIds);
		$topicArr = [];
		foreach ($topicIds as $key => $topicId){
			$topic = (new ResourceTopic())->get($topicId);
			$topicArr[$key]['id'] = $topicId;
			$topicArr[$key]['title'] = $topic['title'];
			$topicArr[$key]['is_hot'] = $topic['is_hot'];
			if ($topicArr[$key]['is_hot'] == 2) {
				unset($topicArr[$key]);
			}
		}
		//新闻类型
		$typeList = (new ResourceType())->where(['type' => 1])->select();
		//新闻类型下的新闻
		$resourceList = (new Resource())->where(['type_id' => $type])->paginate($limit);
		$count = $resourceList->total();

		//总页数
		$totalPages = ceil($count / $limit);

		// 计算起始位置
		$offset = ($page - 1) * $limit;

		// 上一页和下一页的逻辑处理
		$prevPage = $page - 1 < 1 ? 1 : $page - 1;
		$nextPage = $page + 1 > $totalPages ? $totalPages : $page + 1;

		$resourceList = (new Resource())->where(['type_id' => $type])->limit($offset, $limit)->select();

		$resourceArr = [];
		foreach ($resourceList as $val) {
			//热门话题
			$topicIds = (new ResourceTopicRel())->where(['resource_id' => $val['id']])->column('topic_id');
			$arr = [];
			foreach ($topicIds as $topicId){
				$arr[$topicId]['id'] = $topicId;
				$arr[$topicId]['title'] = (new ResourceTopic())->get($topicId)['title'];
			}
			$resourceArr[] = [
				'id'       => $val['id'],
				'title'    => $val['title'],
				'ctime'    => getTime($val['ctime']),
				'brief'    => $val['brief'],
				'pic'      => _sImage($val['pic']),
				'children' => $arr
			];
		}
		$this->assign('hotList', $hotList);
		$this->assign('newList', $newList);
		$this->assign('topicList', $topicArr);
		$this->assign('typeList', $typeList);
		$this->assign('resourceList', $resourceArr);
		$this->assign('type', $type);

		//分页
		$this->assign('count', $count);
		$this->assign('page', $page);
		$this->assign('prevPage', $prevPage);
		$this->assign('nextPage', $nextPage);
		$this->assign('totalPages', $totalPages);

		$employ = (new Link())->get(['position' => 7]);
		$this->assign('employ', $employ);
		return view('news');
	}

	public function detail()
	{
		$id = $this->request->param('id');
		if (isset($id)) {
			$resource = (new Resource())->get($id);
			$resourceList = (new Resource())->where([])->select();
			$resource['pic'] = _sImage($resource['pic']);
			$resource['ctime'] = getTime($resource['ctime']);
			$resource['source'] = config('params.news')['source'][$resource['source']];
			(new Resource())->where(['id' => $id])->setInc('browse', 1);
			//热门话题
			$topicIds = (new ResourceTopicRel())->where(['resource_id' => $id])->column('topic_id');
			$topicArr = [];
			foreach ($topicIds as $topicId){
				$topicArr[$topicId]['id'] = $topicId;
				$topicArr[$topicId]['title'] = (new ResourceTopic())->get($topicId)['title'];
			}
			//上一篇
			$front_id = $id - 1;
			if ($front_id == 0) {
				$front['id'] = $front_id;
				$front['title'] = '没有了';
			} else {
				$front['id'] = $front_id;
				$front['title'] = (new Resource())->get($front_id)['title'];
			}
			//下一篇
			$rear_id = $id + 1;
			$rearOne = (new Resource())->get($rear_id);
			if ($rearOne) {
				$rear['id'] = $rear_id;
				$rear['title'] = $rearOne['title'];
			} else {
				$rear['id'] = 0;
				$rear['title'] = '没有了';
			}
			//热门推荐
			$hotList = (new Resource())->where(['is_hot' => 1])->select();
			foreach ($hotList as $item) {
				$item['pic'] = _sImage($item['pic']);
			}
			$this->assign('hotList', $hotList);
			$this->assign('resource', $resource);
			$this->assign('front', $front);
			$this->assign('rear', $rear);
			$this->assign('resourceList', $resourceList);
			$this->assign('topicList', $topicArr);

			$employ = (new Link())->get(['position' => 9]);
			$this->assign('employ', $employ);
		} else {
			return [
				'code' => -1,
				'msg'  => '缺少参数id'
			];
		}
		return view('detail');
	}

	public function tags()
	{
		return view('tags');
	}

	public function topic()
	{
		$id = $this->request->param('id');
		$topic = (new ResourceTopic())->get($id);
		$resourceIds = (new ResourceTopicRel())->where(['topic_id' => $id])->column('resource_id');
		$resourceIds = array_unique($resourceIds);
		$topicCount = count($resourceIds);
		$resourceArr = [];
		foreach ($resourceIds as $resourceId) {
			$resource = (new Resource())->get($resourceId);
			$topicLists = (new ResourceTopicRel())->where(['resource_id' => $resourceId])->select();
			if (! $topicLists->isEmpty()) {
				foreach ($topicLists as $key =>$list){
					if ($list['topic_id'] == $id) {
						unset($topicLists[$key]);
					}
					$topicLists[$key]['id'] = $list['topic_id'];
					$topicLists[$key]['title'] = (new ResourceTopic())->get($list['topic_id'])['title'];
				}
				$children = $topicLists->toArray();
			} else {
				$children = [];
			}
			$resourceArr[] = [
				'id'       => $resource['id'],
				'title'    => $resource['title'],
				'ctime'    => getTime($resource['ctime']),
				'brief'    => $resource['brief'],
				'pic'      => _sImage($resource['pic']),
				'children' => $children
			];
		}

		//热门推荐
		$hotList = (new Resource())->where(['is_hot' => 1])->select();
		foreach ($hotList as $item) {
			$item['pic'] = _sImage($item['pic']);
		}
		$this->assign('hotList', $hotList);

		//热门话题
		$topicIds = (new ResourceTopicRel())->column('topic_id');
		$topicIds = array_unique($topicIds);
		$topicArr = [];
		foreach ($topicIds as $key => $topicId){
			$topic = (new ResourceTopic())->get($topicId);
			$topicArr[$key]['id'] = $topicId;
			$topicArr[$key]['title'] = $topic['title'];
			$topicArr[$key]['is_hot'] = $topic['is_hot'];
			if ($topicArr[$key]['is_hot'] == 2) {
				unset($topicArr[$key]);
			}
		}
		$this->assign('topicList', $topicArr);

		$this->assign('resourceTopicRelList', $resourceArr);
		$this->assign('topic', $topic);
		$this->assign('topicCount', $topicCount);

		$employ = (new Link())->get(['position' => 10]);
		$this->assign('employ', $employ);
		return view('topic');
	}

	//视界生态
	public function cvmart()
	{
		$type = $this->request->param('type', 3);
		// 当前页码
		$page = $this->request->param('page', 1);
		// 每页显示的数量
		$limit = 10;
		//热门
		$hotList = (new Resource())->where(['is_hot' => 1])->select();
		foreach ($hotList as $item) {
			$item['pic'] = _sImage($item['pic']);
		}
		//最新
		$newList = (new Resource())->where(['is_new' => 1])->select();
		foreach ($newList as $item) {
			$item['pic'] = _sImage($item['pic']);
		}
		//热门话题
		$topicIds = (new ResourceTopicRel())->column('topic_id');
		$topicIds = array_unique($topicIds);
		$topicArr = [];
		foreach ($topicIds as $key => $topicId){
			$topic = (new ResourceTopic())->get($topicId);
			$topicArr[$key]['id'] = $topicId;
			$topicArr[$key]['title'] = $topic['title'];
			$topicArr[$key]['is_hot'] = $topic['is_hot'];
			if ($topicArr[$key]['is_hot'] == 2) {
				unset($topicArr[$key]);
			}
		}
		//新闻类型
		$typeList = (new ResourceType())->where(['type' => 2])->select();
		//新闻类型下的新闻
		$resourceList = (new Resource())->where(['type_id' => $type])->paginate($limit);
		$count = $resourceList->total();

		//总页数
		$totalPages = ceil($count / $limit);

		// 计算起始位置
		$offset = ($page - 1) * $limit;

		// 上一页和下一页的逻辑处理
		$prevPage = $page - 1 < 1 ? 1 : $page - 1;
		$nextPage = $page + 1 > $totalPages ? $totalPages : $page + 1;

		$resourceList = (new Resource())->where(['type_id' => $type])->limit($offset, $limit)->select();

		$resourceArr = [];
		foreach ($resourceList as $val) {
			//热门话题
			$topicIds = (new ResourceTopicRel())->where(['resource_id' => $val['id']])->column('topic_id');
			$arr = [];
			foreach ($topicIds as $topicId){
				$arr[$topicId]['id'] = $topicId;
				$arr[$topicId]['title'] = (new ResourceTopic())->get($topicId)['title'];
			}
			$resourceArr[] = [
				'id'       => $val['id'],
				'title'    => $val['title'],
				'ctime'    => getTime($val['ctime']),
				'brief'    => $val['brief'],
				'pic'      => _sImage($val['pic']),
				'children' => $arr
			];
		}
		$this->assign('hotList', $hotList);
		$this->assign('newList', $newList);
		$this->assign('topicList', $topicArr);
		$this->assign('typeList', $typeList);
		$this->assign('resourceList', $resourceArr);
		$this->assign('type', $type);

		//分页
		$this->assign('count', $count);
		$this->assign('page', $page);
		$this->assign('prevPage', $prevPage);
		$this->assign('nextPage', $nextPage);
		$this->assign('totalPages', $totalPages);

		$employ = (new Link())->get(['position' => 8]);
		$this->assign('employ', $employ);
		return view('cvmart');
	}
}
